package kz.pompei.OneGwt.ifaces.widget.gwtadapters;

import kz.pompei.OneGwt.ifaces.util.Handler;
import kz.pompei.OneGwt.ifaces.util.HandlerKiller;
import kz.pompei.OneGwt.ifaces.widget.ButtonWidget;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Button;

public class Button_impl_ButtonWidget implements ButtonWidget {
  
  private final Button button;
  
  public Button_impl_ButtonWidget(Button button) {
    this.button = button;
  }
  
  @Override
  public void setVisible(boolean visible) {
    button.setVisible( visible );
  }
  
  @Override
  public void setEnabled(boolean enabled) {
    button.setEnabled( enabled );
  }
  
  @Override
  public void setFocus(boolean focused) {
    button.setFocus( focused );
  }
  
  @Override
  public HandlerKiller addClickHandler(final Handler handler) {
    final HandlerRegistration hr = button.addClickHandler( new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        handler.handle();
      }
    } );
    
    return new HandlerKiller() {
      @Override
      public void killHandler() {
        hr.removeHandler();
      }
    };
  }
  
}
